<template>
  <div class></div>
</template>
<script>
import axios from "@/utils/requestdp";

export default {
  name: "APIqueryDbzhongyaojiance",
  components: {},
  data() {
    return {};
  },
  computed: {
    ishistory: function () {
      return this.$store.state.daping.ishistorytime;
    },
    timerangarr: function () {
      return this.$store.state.daping.timepick;
    },
  },
  watch: {
    "$store.state.daping.bigScreen": {
      handler: function (n, o) {
        this.watchEvent();
      },
    },
  },
  mounted() {},
  methods: {
    watchEvent() {
      //先清除定时任务
      if (
        this.$store.state.daping.intervalArr["APIqueryDbzhongyaojiance"] !=
        undefined
      ) {
        return;
      }
      let n_tmp = this.$store.state.daping.bigScreen;
      let g_bigscreen_data_arr = n_tmp.filter((item) => {
        if (item.attrName == "monitor_pids") {
          return true;
        }
      });
      this.loadData(g_bigscreen_data_arr[0].attrValue);
      let interval = setInterval(() => {
        let n_tmp = this.$store.state.daping.bigScreen;
        let g_bigscreen_data_arr = n_tmp.filter((item) => {
          if (item.attrName == "monitor_pids") {
            return true;
          }
        });
        this.loadData(g_bigscreen_data_arr[0].attrValue);
      }, 1000 * 30);
      this.$store.state.daping.intervalArr["APIqueryDbzhongyaojiance"] = [
        { interval: interval },
      ];
    },
    loadData(pidStr) {
      let _this = this;

      var g_his_stime = 1579150860000; //2020.01.16 13:01:00
      var g_his_etime = 1579151160000; //2020.01.16 13:06:00 13:05:00有一条数据

      //热力站：供压m_001p 回压m_002p
      //计量站：供压m_006p 回压m_007p
      var pidArr = [];
      var pidNameObj = new Object();
      var pidStrArr = pidStr.split(",");
      for (var i = 0; i < pidStrArr.length; i++) {
        // var type = pidStrArr[i].split("@@")[0];
        var pid = pidStrArr[i].split("@@")[1];
        var name = pidStrArr[i].split("@@")[2];
        pidArr.push(pid);
        pidNameObj[pid] = name;
      }

      let queryJson = {
        queries: [
          {
            metric: "pid,m_006p,m_007p",
            schema: "monitor",
          },
        ],
        table: "measuredpoint",
        search: "pid=" + pidArr.toString(),
      };
      if (this.ishistory == "历史") {
        queryJson = {
          queries: [
            {
              metric: "pid,m_006p,m_007p",
              schema: "monitor",
            },
          ],
          table: "measuredpoint",
          search: "pid=" + pidArr.toString(),
          type: "history",
          startTime: this.timerangarr[0].getTime(),
          endTime: this.timerangarr[1].getTime(),
        };
      }

      var importMonitorData = [];

      axios.post("/open/queryDb", queryJson).then((res1) => {
        let res = {
          data: {
            200: res1.data
          }
        }
        importMonitorData = importMonitorData.concat(
          res.data["200"] ? res.data["200"] : []
        );

        let queryJson1 = {
          queries: [
            {
              metric: "pid,m_001p,m_002p",
              schema: "monitor",
            },
          ],
          table: "substation",
          search: "pid=" + pidArr.toString(),
        };
        if (this.ishistory == "历史") {
          queryJson1 = {
            queries: [
              {
                metric: "pid,m_001p,m_002p",
                schema: "monitor",
              },
            ],
            table: "substation",
            search: "pid=" + pidArr.toString(),
            type: "history",
            startTime: this.timerangarr[0].getTime(),
            endTime: this.timerangarr[1].getTime(),
          };
        }
        axios.post("/open/queryDb", queryJson1).then((res1) => {
          let res = {
          data: {
            200: res1.data
          }
        }
          importMonitorData = importMonitorData.concat(
            res.data["200"] ? res.data["200"] : []
          );

          var gongData = [],
            huiData = [];
          for (var j = 0; j < pidArr.length; j++) {
            //【2019年12月4日】为了要保持顺序 不然一刷新名字就乱跳
            for (var i in importMonitorData) {
              if (pidArr[j] == importMonitorData[i]["pid"]) {
                gongData.push([
                  pidNameObj[importMonitorData[i]["pid"]],
                  importMonitorData[i]["m_001p"] ||
                  importMonitorData[i]["m_001p"] == 0
                    ? importMonitorData[i]["m_001p"]
                    : importMonitorData[i]["m_006p"] ||
                      importMonitorData[i]["m_006p"] == 0
                    ? importMonitorData[i]["m_006p"]
                    : 0,
                ]);
                huiData.push([
                  pidNameObj[importMonitorData[i]["pid"]],
                  importMonitorData[i]["m_002p"] ||
                  importMonitorData[i]["m_002p"] == 0
                    ? importMonitorData[i]["m_002p"]
                    : importMonitorData[i]["m_007p"] ||
                      importMonitorData[i]["m_007p"] == 0
                    ? importMonitorData[i]["m_007p"]
                    : 0,
                ]);
              }
            }
          }

          //供回数据
          _this.$store.state.daping.queryDbMeasuredpoint = {
            gongData: this.avr(gongData),
            huiData: this.avr(huiData),
          };

        });
      });
    },
    avr(val) {

      let gongData = val;
      let gongdataf = {};
      let gongdataff = [];

      let total = gongData.filter((item) => {
        return gongData[0][0] == item[0];
      }).length;

      gongData.map((item) => {
        if (!gongdataf[item[0]]) {
          gongdataf[item[0]] = {total: 1, count: 0}
          gongdataf[item[0]].count = gongdataf[item[0]].count + item[1];
        } else {
          gongdataf[item[0]].count = Number(item[1]) + Number(gongdataf[item[0]].count);
          gongdataf[item[0]].total++
        }
      });
      _.forIn(gongdataf, (item,index) =>{
        gongdataff.push([index,Number(item.count/item.total).toFixed(2)])
      })
      return gongdataff
    },
  },
};
</script>
<style lang='scss' scoped>
</style>